package com.runmi.webchat.aftersale.web.shop.service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import com.runmi.webchat.aftersale.common.result.Result;
import com.runmi.webchat.aftersale.common.result.ResultFactory;
import com.runmi.webchat.aftersale.common.utils.MD5;
import com.runmi.webchat.aftersale.dbcore.dao.ShopInfoDao;
import com.runmi.webchat.aftersale.dbcore.entity.ShopInfo;
import com.runmi.webchat.aftersale.dbcore.entity.User;
import com.runmi.webchat.aftersale.dbcore.entity.UserOpenId;
import com.runmi.webchat.aftersale.dbcore.mapper.UserOpenIdMapper;
import com.runmi.webchat.aftersale.util.Constants;
import com.runmi.webchat.aftersale.web.base.BaseService;
import com.runmi.webchat.aftersale.web.shop.vo.ShopManageEditReqVo;
import com.runmi.webchat.aftersale.web.shop.vo.ShopManageIndexReqVo;
import com.runmi.webchat.aftersale.web.shop.vo.ShopUserEdit;
import com.runmi.webchat.aftersale.web.shop.vo.ShopUserIndexReqVo;

@Service
public class ShopUserService extends BaseService{

	@Autowired
	private UserOpenIdMapper userOpenIdMapper;
	
	@Autowired
	private ShopInfoDao shopInfoDao;
	
	public void index(ShopUserIndexReqVo model){
			
			try{		
					
			    PageHelper.startPage(model.getCpage(), model.getPageSize());
			    List<Map> items = 
			    		userOpenIdMapper.getUserOpenInfoByCondition(
			    				model.getRealName(), 
			    				model.getShopName(), 
			    				model.getShopId(),null,null,null);
					PageInfo<Map> pageInfo = new PageInfo<Map>(items);
				model.setTotalCount((int)pageInfo.getTotal());
				model.setItems(items);
			}catch(Exception ex){
				ex.printStackTrace();
				
			}
		}
	
	public Result audit(Integer userId,Byte opType){
		Result result= ResultFactory.getResultFail();
		UserOpenId model= userOpenIdMapper.selectByPrimaryKey(userId);
		if(null==model){	
			result=ResultFactory.getResultFail("未查询到用户");
			return result;
		}
		
		UserOpenId record=new UserOpenId();
		record.setId(model.getId());
		record.setStatus(opType);;					
		userOpenIdMapper.updateByPrimaryKeySelective(record);
		
		result=	ResultFactory.getResultSuccess();
		return result;
	}
	
	public void edit(ShopUserEdit model){
		
		model.setShopInfos(shopInfoDao.getAllShopInfoList());
		
		UserOpenId userOpen= null;
		if(null!=model.getUserId()){
			userOpen=userOpenIdMapper.selectByPrimaryKey(model.getUserId());
		}
		userOpen=userOpen==null?new UserOpenId():userOpen;
		model.setUserOpen(userOpen);
		
	}
	
	@Transactional
	public Result save(
			Integer id, 
			Integer shopId,
			String usrName,
			String realName, String mobile, 
			String pwd,
			User currentUser) {
		try {
			

			Date currentDate = Calendar.getInstance().getTime();
			
			if (null == id) {
				UserOpenId userOpen = new UserOpenId();
				userOpen.setUsrName(usrName);
				userOpen.setRealName(realName);
				userOpen.setShopId(shopId);
				userOpen.setMobile(mobile);
				if(StringUtil.isNotEmpty(pwd)){
					String psdmd5=MD5.getMD5(pwd);
					userOpen.setPwd(psdmd5);
				}else{
					String psdmd5=MD5.getMD5("111111");
					userOpen.setPwd(psdmd5);
				}
				userOpen.setStatus((byte) 1);
				userOpen.setCreateTime(new Date());
				userOpen.setDelFlag((byte)0);
				userOpenIdMapper.insert(userOpen);
			} else {
				UserOpenId userOpen = userOpenIdMapper.selectByPrimaryKey(Integer.valueOf(id));
				userOpen.setUsrName(usrName);
				userOpen.setRealName(realName);
				userOpen.setShopId(shopId);
				userOpen.setMobile(mobile);
				if(StringUtil.isNotEmpty(pwd)){
					String psdmd5=MD5.getMD5(pwd);
					userOpen.setPwd(psdmd5);
				}
				userOpenIdMapper.updateByPrimaryKeySelective(userOpen);
			}

			
			return ResultFactory.getResultSuccess();

		} catch (Exception e) {
			logErr.error("保存客户档案参数异常", e);	
			return ResultFactory.getResultFail("系统错误");

		}
	}
}
